<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/master' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>营销管理</el-breadcrumb-item>
                <el-breadcrumb-item>广告位置</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="advPosFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="advPosFormData.name" placeholder="请输入位置"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="initAdvPos()">查询</el-button>
                    <el-button type="primary" @click="insertAdvPos">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="type"
                            label="类型"
                            width="180">
                    </el-table-column>

                    <el-table-column
                            prop="name"
                            label="广告位置名称">
                    </el-table-column>
                    <el-table-column
                            prop="descrition"
                            label="广告说明">
                    </el-table-column>

                    <el-table-column
                            prop="maxCount"
                            label="广告数量">
                    </el-table-column>
                    
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" @click="selById(scope.row.id)" circle></el-button>
                            <el-button type="danger" icon="el-icon-delete" @click="del(scope.row.id)" circle></el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- 分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>

        <!--        更新信息表单-->
        <el-dialog
                title="添加广告位置信息"
                :visible.sync="editAdvPos"
                width="30%"
                :before-close="cha"
        >
            <!--          model表单数据-->
            <el-form  label-width="80px" :model="advPosForm" :rules="advPosFormRules" ref="advPosFormRef">
                <el-form-item label="类型" prop="type">
<!--                    <el-input v-model="advPosForm.type"></el-input>-->
                    <el-select v-model="advPosForm.type" placeholder="请选择类型">
                        <!--                    <el-option label="" value=""></el-option>-->
                        <el-option label="横幅广告" value="横幅广告"></el-option>
                        <el-option label="文字链广告" value="文字链广告"></el-option>
                        <el-option label="富媒体广告" value="富媒体广告"></el-option>
                        <el-option label="视频广告" value="视频广告"></el-option>
                        <el-option label="移动广告" value="移动广告"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="广告位置名称" prop="name">
                    <el-input v-model="advPosForm.name"></el-input>
                </el-form-item>
                <el-form-item label="广告说明" prop="descrition">
                    <el-input v-model="advPosForm.descrition"></el-input>
                </el-form-item>
                <el-form-item label="广告数量" prop="maxCount">
                    <el-input v-model="advPosForm.maxCount"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                        <el-button @click="qx">取 消</el-button>
                        <el-button type="primary" @click="editAdvPosBtn">确 定</el-button>
                    </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "AdverPosition",
        data(){
            return {
                //条件搜索对象
                advPosFormData:{},
                //表格数据
                tableData:[],
                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage:1,
                //总条数
                total:0,
                //更新用户弹出框显示状态
                editAdvPos:false,
                //添加用户表单信息,加校验
                advPosForm:{
                    type:"",
                    name:"",
                    descrition:"",
                    maxCount:"",
                },
                // 用户表单校验数据:
                advPosFormRules:{
                    type:[{required:true,message:"类型不可为空",trigger:'blur'}],
                    name:[{ required: true, message: '广告位置名称不可为空',trigger: 'blur'}],
                    descrition:[{ required: true, message: '广告说明不可为空',trigger: 'blur'}],
                    maxCount:[{ required: true, message: '广告数量不可为空',trigger: 'blur'}],
                },
            }
        },
        created() {
            //获取用户的信息
            this.initAdvPos();

        },
        methods:{
            initAdvPos(){
                //get方式不支持json数据
                var that=this;
                this.$http.post(`/marketing/position/selectAdvPosit/${this.currentPage}/${this.pageSize}`,this.advPosFormData).then(function (re) {
                    that.tableData=re.data.result.records;
                    that.total=re.data.result.total;

                })
            },
            //当改变没有显示的条数时触发该方法
            handleSizeChange(v){
                this.pageSize=v
                this.initAdvPos();
            },
            //切换页码时触发该方法
            handleCurrentChange(v){
                this.currentPage=v
                this.initAdvPos();
            },
            //点击添加按钮
            insertAdvPos(){
                this.editAdvPos=true;
                //将form表单数据清空,以防止修改时数据回显后表单中有数据冗余
                this.advPosForm={};

            },
            //更新用户弹出框确定按钮事件
            editAdvPosBtn(){
                var that = this;
                this.$refs.advPosFormRef.validate((valid) => {
                    if (valid) {
                        if (this.advPosForm.id != null) {
                            this.$http.put(`/marketing/position/updateAdvPosit`, this.advPosForm).then(function (r) {
                                that.$message.success(r.data.message);
                                that.editAdvPos = false;
                                that.initAdvPos();
                            })
                        } else {
                            this.$http.post(`/marketing/position/insertAdvPosit`, this.advPosForm).then(function (r) {
                                that.$message.success(r.data.message);
                                that.editAdvPos = false;
                                that.initAdvPos();

                            })
                        }
                        this.$refs.advPosFormRef.resetFields();
                    }
                })
            },

            //点击修改根据id进行回显
            selById(id){
                var that=this;
                this.editAdvPos=true;
                this.$http.get(`/marketing/position/byId/${id}`).then(function (r) {
                    that.advPosForm=r.data.result.advPosition;
                })
            },
            del(id){
                var that=this;
                this.$http.delete(`/marketing/position/deleteAdvPosit/${id}`).then(function (r) {
                    that.initAdvPos();
                    // location.reload();
                })
                this.currentPage=1;
            },
            //弹出框的取消按钮,重置数据,关闭弹窗
            qx(){
                // this.$refs.advPosFormRef.resetFields();
                this.editAdvPos=false;
            },
            //×按钮
            cha(){
                this.$refs.advPosFormRef.resetFields();
                this.editAdvPos=false;
            },

        }
    }
</script>

<style scoped>

</style>